home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / fractal / kaos.lha / graphlib / graph_proc.c next >
Encoding:
C/C++ Source or Header  |  1989-12-19  |  3.5 KB  |  133 lines

  1. /*
  2. ### pipe the output to a graph  filter ###
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <suntool/sunview.h>
  7. #include <suntool/textsw.h>
  8. #include <suntool/panel.h>
  9. #include <sunwindow/notify.h>
  10.  
  11. int graph_logx=0,graph_logy=1;
  12. int graph_childpid,graph_fromchild,graph_tochild;
  13. FILE *fp_graph_fromchild,*fp_graph_tochild;
  14. int graph_owner=0; /* should be set from calling routines */
  15.  
  16. void graph_proc(str)
  17. char str[];
  18. {
  19.     void graph_pipe_create(),graph_register_functions();
  20.     extern int graph_owner;
  21.     extern FILE *fp_graph_fromchild,*fp_graph_tochild;
  22.     
  23.     graph_pipe_create(str,&graph_childpid,&graph_fromchild,&graph_tochild);
  24.     graph_register_functions(graph_owner);
  25. }
  26.  
  27. void graph_register_functions(owner)
  28. int owner;
  29. {
  30.     static Notify_value graph_pipe_reader(),graph_dead_child();
  31.     Panel_item item;
  32.     extern Panel_item fft_go_item,dims_go_item;
  33.  
  34.     if(owner==0){
  35.         item = fft_go_item;
  36.     }
  37.     else if (owner==1){
  38.     }
  39.     else if (owner==2){
  40.         item = dims_go_item;
  41.     }
  42.         (void) notify_set_input_func(item,graph_pipe_reader,graph_fromchild);
  43.         (void) notify_set_wait3_func(item,graph_dead_child,graph_childpid);
  44. }
  45.  
  46. /*
  47. ### Read the input pending on the pipe ###
  48. */ 
  49.  
  50. static Notify_value graph_pipe_reader(item,fd)
  51. Panel_item item;
  52. int fd;
  53. {
  54.     extern FILE *fp_graph_fromchild;
  55.  
  56.     return(NOTIFY_DONE);
  57. }
  58.  
  59. /*
  60. ### Close the pipe and let the notifier know if the child process died ###
  61. */
  62.  
  63. static Notify_value graph_dead_child(item,pid,status,rusage)
  64. Panel_item item;
  65. int     pid;
  66. union wait *status;
  67. struct rusage *rusage;
  68. {
  69.     extern int graph_fromchild;
  70.         (void) notify_set_input_func(item,NOTIFY_FUNC_NULL,graph_fromchild);
  71.         close(graph_fromchild);
  72.         return(NOTIFY_DONE);
  73. }
  74.  
  75. /*
  76. ### create pipes ###
  77. */
  78. void graph_pipe_create(s,pid,from,to)
  79. char s[];
  80. int *pid,*from,*to;
  81. {
  82.     int pipeto[2],pipefrom[2];
  83.     int c,numfds;
  84.     FILE *popen();
  85.     
  86.     /* Create a pipe */
  87.         if(pipe(pipeto) < 0){
  88.                 perror("new process");
  89.         system_mess_proc(1,"Error: Pipe failed! Check the file name!");
  90.         return;
  91.     }
  92.     if(pipe(pipefrom) < 0) {
  93.                 perror("new process");
  94.         system_mess_proc(1,"Error: Pipe failed! Check the file name!");
  95.         return;
  96.     }
  97.  
  98.     /* Fork a child */
  99.         switch (*pid = fork()){
  100.                 case -1:
  101.                         perror("new process");
  102.                         exit(1);
  103.                 case 0:
  104.                         /* use dup2 to set the child's stdin and stdout to
  105.                         the pipe */
  106.                         dup2(pipeto[0],0);
  107.                         dup2(pipefrom[1],1);
  108.  
  109.                         /* close all other fds (except stderr) since child
  110.                         process doesn't know about or need them */
  111.                         numfds = getdtablesize();
  112.                         for (c = 3; c < numfds; c++)
  113.                                 close(c);
  114.                         (void) execl("/bin/csh","csh","-c",s,0);
  115.                         perror("new process: child");
  116.                         exit(1);
  117.                 default:
  118.                         close(pipeto[0]);
  119.                         close(pipefrom[1]);
  120.                         *to = pipeto[1];
  121.                         fp_graph_tochild = fdopen(*to, "w");
  122.                         *from = pipefrom[0];
  123.                         fp_graph_fromchild = fdopen(*from, "r");
  124.  
  125.                         /* The pipe should be unbuffered or "new process" 
  126.                         will not get any data until 1024 characters have
  127.                         been sent */
  128.                         setbuf(fp_graph_tochild,NULL);
  129.                         setbuf(fp_graph_fromchild,NULL);
  130.                         break;
  131.         }
  132. }
  133.